<?php

namespace plugin\webman\scissors;
use support\Db;
use GatewayWorker\Lib\Gateway;
use support\Redis;
use plugin\webman\common\Common;

// 剪刀石头布
class Scissors
{
    // 创建房间
   public static function createroom($client_id,$data){
        $checktoken =   Common::checktoken($client_id,$data['uid'],$data['token']);
        if($checktoken==1){
            // 判断用户当前是否在其他房间
            $if_in_room =  Method::if_in_room($client_id,$data['uid']);
            // 创建房间
            $create = Method::createroom($client_id,$data);
            $res = [
                'code'=>1,
                'msg'=>'create room success',
                'info'=>$create
            ];
            return $res;

        }
    }

    // 关闭/退出房间
    public static function closeroom($client_id,$data){
        $roominfo = Db::table("wa_scissors_rooms")->where("roomno",$data['roomno'])->first();
        
        if($roominfo->createuid == $data['uid']){
            // 房主关闭房间
            $close = Method::creater_closeroom($client_id,$data);
            $res = [
                'code'=>1,
                'msg'=>'关闭房间成功',
                'info'=>[]
            ];
            // Gateway::sendToGroup($data['roomno'],Common::returndata('roomclosed_by_fangzhu',$res));
             // 发送群组通知
            Gateway::sendToGroup($data['roomno'],Common::returndata('roomclosed_by_fangzhu',$res));
            return $res;
        }
        if($roominfo->joinuid == $data['uid']){
            // 加入者退出房间
            $close = Method::joinr_closeroom($client_id,$data);
        }       

    }

    // 加入房间
    public static function joinroom($client_id,$data){
        // 判断房间号是否存在
        $roominfo = Db::table("wa_scissors_rooms")->where("roomno",$data['roomno'])->first();
        // var_dump($roominfo);die;
        if($roominfo == ''){
            $res = [
                'code'=>1060,
                'msg'=>'房间号错误',
                'info'=>[]
            ];
            return $res;
        }
        // var_dump($roomid);die;
        // 判断房间是否已满
        if($roominfo->joinuid != ''){
            $res = [
                'code'=>1061,
                'msg'=>'房间号人数已满',
                'info'=>[]
            ];
            return $res;            
        }
        // 加入房间
        $up = Db::table('wa_scissors_rooms')->where("roomno",$data['roomno'])->update(['joinuid'=>$data['uid'],'join_time'=>time()]);
        if($up){
            $room_users = Method::get_room_users($data['roomno']);
            // 把成员加入 gateway群组
            Gateway::joinGroup($client_id, $data['roomno']);
            
            $res = [
                'code'=>1,
                'msg'=>'join room success',
                'info'=>$room_users
            ];
            Gateway::sendToGroup($data['roomno'],Common::returndata('userjoin',$res));
            return $res;     
        }
    }


    // 连接断开 自动退出房间

    public static function Oncloseroom($client_id,$uid){
        $roominfo = Db::table("wa_scissors_rooms")->where("joinuid",$uid)->first();
        if($roominfo->id == ''){
            // 是创建者
            
            $res = [
                'code'=>1052,
                'msg'=>'房主已关闭本房间',
                'info'=>$roominfo->roomno
            ];
            Gateway::sendToGroup($roominfo->roomno,Common::returndata('roomclosed',$res));
            $del = Db::table("wa_scissors_rooms")->where("joinuid",$uid)->delete();
        }else{
            //是参与者
            $up = Db::table("wa_scissors_rooms")->where("roomno",$roominfo->roomno)->update(['joinuid'=>'','join_time'=>0]);
            $res = [
                'code'=>1058,
                'msg'=>$uid.'已退出房间',
                'info'=>$roominfo->roomno
            ];
             // 发送群组通知
            Gateway::sendToGroup($roominfo->roomno,Common::returndata('userout',$res));
        }
    }

    // 房主设置积分
    public static function setsocre($client_id,$data){
        $roominfo = Db::table("wa_scissors_rooms")->where("roomno",$data['roomno'])->first();
        if($roominfo->createuid !=  $data['uid']){
            $res = [
                'code'=>1068,
                'msg'=>'只有房主可操作设定',
                'info'=>[]
            ];
            return $res;
        }

        $roominfoup = Db::table("wa_scissors_rooms")->where("roomno",$data['roomno'])->update(['score'=>$data['score']]);
        if($roominfoup){
            $roominfo2 = Db::table("wa_scissors_rooms")->where("roomno",$data['roomno'])->first();
            $res = [
                'code'=>1,
                'msg'=>'积分设置成功',
                'info'=>$roominfo2 
            ];
            
            return $res;
        }else{
            $res = [
                'code'=>1069,
                'msg'=>'积分设置失败',
                'info'=>[]
            ];
            return $res;
        }
    }

    // 准备游戏
    public static function ready($client_id,$data){
        $roominfo = Db::table("wa_scissors_rooms")->where("roomno",$data['roomno'])->first();
        Gateway::joinGroup($client_id, $data['roomno']);
        //  // 判断余额是否足够
        $userinfo =Common::getuserinfo($data['uid']);
        if($userinfo['coin'] < $roominfo->score){
            $res = [
                'code'=>1052,
                'msg'=>'余额不足，准备失败',
                'info'=>[]
            ];
            return $res;
        }
        if($roominfo->createuid == $data['uid']){
            
            Db::table("wa_scissors_rooms")->where("roomno",$data['roomno'])->update(['createstatus'=>1,'createtype'=>$data['type']]);
            $roominfo2 = Db::table("wa_scissors_rooms")->where("roomno",$data['roomno'])->first();
            $res = [
                'code'=>1,
                'msg'=>'积分设置成功',
                'info'=>$roominfo2 
            ];
            Gateway::sendToGroup($data['roomno'],Common::returndata('scorechange',$res));
        }else{
            Db::table("wa_scissors_rooms")->where("roomno",$data['roomno'])->update(['joinstatus'=>1,'jointype'=>$data['type']]);
        }

        // 判断双方是否都准备完成
        $allready = Method::ifallready($data['roomno']);
        $res = [
            'code'=>1,
            'msg'=>'准备成功',
            'info'=>[]
        ];
        return $res;
    }


    // 分润
    public static function fenrun($client_id,$data){
        $roominfo = Db::table("wa_scissors_rooms")->where("roomno",$data['roomno'])->first();
        if($data['uid'] != $roominfo->createuid){
            $res = [
                'code'=>1201,
                'msg'=>'无权限',
                'info'=>[]
            ];
            return $res;
        }
        if($data['winnerid'] != 0){
            Common::updateusercoin($data['winnerid'],$roominfo->score,1);
        }
        if($data['loserid'] != 0){
            Common::updateusercoin($data['loserid'],$roominfo->score,0);
        }
        // 更改准备状态
        Db::table("wa_scissors_rooms")->where("roomno",$data['roomno'])->update(['joinstatus'=>0,'createstatus'=>0,'score'=>0,'createtype'=>0,'jointype'=>0]);

        Db::table("wa_usercoin_record")->insert([
            'uid'=>$data['winnerid'],
            'coin'=>$roominfo->score,
            'type'=>1,
            'remark'=>'剪刀石头布赢取',
            'addtime'=>date("Y-m-d H:i:s")
        ]);
        $info = Method::get_room_users($data['roomno']);
        $res = [
            'code'=>1,
            'msg'=>'分润成功',
            'info'=>$info
        ];
        Gateway::sendToGroup($data['roomno'],Common::returndata('returnfenrun',$res));
        return $res;
    }

}